package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.specGroupService.queryGroupByCategoryId(id));
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> queryParam(
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "groupId",required = false)Long groupId,
            @RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(this.specParamService.queryParam(categoryId,groupId,searching));
    }

    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO groupDTO){
        this.specGroupService.saveGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO groupDTO){
        this.specGroupService.updateGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO body){
        this.specParamService.saveParam(body);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO body){
        this.specParamService.updateParam(body);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpec(@RequestParam("id")Long categoryId){
        return ResponseEntity.ok(this.specGroupService.querySpec(categoryId));
    }
}
